package com.frkj.yuan.main;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.StrictMode;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.frkj.yuan.api.bean.UserInforBean;
import com.frkj.yuan.utils.CardUtils;
import com.frkj.yuan.view.PictureSelectorEngineImp;
import com.luck.picture.lib.app.IApp;
import com.luck.picture.lib.app.PictureAppMaster;
import com.luck.picture.lib.engine.PictureSelectorEngine;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：菲菲 on 2022/2/11 09:56
 * <p>
 * company：中星电子
 */
public class BaseApplication extends Application implements IApp {
    private static BaseApplication mContext;
    private static List<Activity> activityList = new ArrayList<>();
    protected static UserInforBean userInforBean = null;


    public static void setmContext(BaseApplication mContext) {
        BaseApplication.mContext = mContext;
    }

    public UserInforBean getUserInforBean() {
        return this.userInforBean ;
    }

    public void CachUserInforBean(UserInforBean userInforBean) {
        this.userInforBean = userInforBean;
    }
    //用一个静态全局变量来统一控制是否打印日志
    public static BaseApplication getContext() {
        return mContext;
    }

    //设置下拉刷新和上拉加载构建器
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context);
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        PictureAppMaster.getInstance().setApp(this);
        // android 7.0系统解决拍照闪退的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        // 初始化MultiDex
        MultiDex.install(this);
        CardUtils.init();
    }

    /**
     * 添加Activity
     */
    public void addActivity(Activity activity) {
        /**
         * 判断当前集合中不存在该Activity
         * 把当前Activity添加到集合中
         */
        if (!activityList.contains(activity)) {
            activityList.add(activity);
        }
    }
    /**
     * 销毁单个Activity
     */
    public static void removeActivity(Activity activity) {
        activityList.remove(activity);
        if (!activity.isFinishing()) {
            activity.finish();
        }
    }
    /**
     * 销毁所有的Activity
     */
    public static void removeALLActivity() {
        /***
         * 通过循环，把集合中的所有Activity销毁
         */
        for (Activity activity : activityList) {
            activity.finish();
        }
    }
    @Override
    public Context getAppContext() {
        return this;
    }

    @Override
    public PictureSelectorEngine getPictureSelectorEngine() {
        return new PictureSelectorEngineImp();
    }
}
